/*
 * Copyright (C) 2015-2021 Alibaba Group Holding Limited
 */

#include <stdio.h>
#include <stdlib.h>
#include <ulog/ulog.h>
#include <cx_debug.h>
#include "cx_hal.h"

#define TAG "cxchip"

static cx_hal_cbs_t *g_cx_chip_cbs = NULL;

int cx_register_chip_cbs(cx_hal_cbs_t *cbs)
{
    CHECK_PARAM(cbs, -1);

    g_cx_chip_cbs = (cx_hal_cbs_t *)calloc(1, sizeof(cx_hal_cbs_t));
    memcpy(g_cx_chip_cbs, cbs, sizeof(cx_hal_cbs_t));

    return 0;
}

int cx_unregister_chip_cbs(void)
{
    free(g_cx_chip_cbs);
    g_cx_chip_cbs = NULL;
    return 0;
}

int cx_get_raw_image(int cam_id, char *data, cx_raw_img_type_e *type, int *width, int *height)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->get_raw_image && 
        cam_id >=0 && data && type && width && height, -1);

    return g_cx_chip_cbs->get_raw_image(cam_id, data, type, width, height);   
}

int cx_start_video_encode(int start)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->start_video_encode, -1);
    LOGD(TAG, "%s %d", __func__, start);
    return g_cx_chip_cbs->start_video_encode(start);
}

int cx_start_audio_encode(int start)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->start_audio_encode, -1);
    LOGD(TAG, "%s %d", __func__, start);
    return g_cx_chip_cbs->start_audio_encode(start);
}

int cx_get_video_attributes(cx_encvideo_format_t *format)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->get_video_attrs && format, -1);

    return g_cx_chip_cbs->get_video_attrs(format);
}

int cx_get_audio_attributes(cx_encaudio_format_t *format)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->get_audio_attrs && format, -1);

    return g_cx_chip_cbs->get_audio_attrs(format);
}

int cx_set_playvideo_attributes(cx_encvideo_format_t *format)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->set_playvideo_attrs && format, -1);

    return g_cx_chip_cbs->set_playvideo_attrs(format);
}

int cx_set_playaudio_attributes(cx_encaudio_format_t *format)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->set_playaudio_attrs && format, -1);

    return g_cx_chip_cbs->set_playaudio_attrs(format);
}

int cx_get_encoded_video(void *data, uint32_t max_size, int *is_iframe, long long *timestamp)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->get_encoded_video && data, -1);

    return g_cx_chip_cbs->get_encoded_video(data, max_size, is_iframe, timestamp);
}

int cx_get_encoded_audio(void *data, long long *timestamp)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->get_encoded_audio && data, -1);

    return g_cx_chip_cbs->get_encoded_audio(data, timestamp);
}

int cx_play_encoded_audio(void *data, uint32_t size, long long *timestamp)
{
    CHECK_PARAM(g_cx_chip_cbs && g_cx_chip_cbs->play_encoded_audio && data, -1);

    return g_cx_chip_cbs->play_encoded_audio(data, size, timestamp);
}

void cx_video_seek_iframe(int timeout_ms)
{
    return g_cx_chip_cbs->video_seek_iframe(timeout_ms);
}

int cx_get_device_id(char *dev_id)
{
    CHECK_PARAM(dev_id, -1);
    return g_cx_chip_cbs->get_device_id(dev_id);
}
